-- ============================================
-- TEST MANETTE (multi-integrator) - diagnostic
-- Detecte TOUS les Redstone Integrator connectes au reseau, et
-- affiche en direct lequel (+ quelle face) s'active quand tu
-- appuies sur un bouton de la telecommande.
-- ============================================

local names = peripheral.getNames()
local integrators = {}

for _, name in ipairs(names) do
    if peripheral.getType(name) == "redstoneIntegrator" then
        table.insert(integrators, { name = name, obj = peripheral.wrap(name) })
    end
end

if #integrators == 0 then
    error("Aucun Redstone Integrator trouve sur le reseau ! Verifie le Wired Modem.")
end

local SIDES = { "top", "bottom", "left", "right", "front", "back" }

term.clear()
term.setCursorPos(1, 1)
print("=== TEST MANETTE (multi-integrator) ===")
print(#integrators .. " Redstone Integrator(s) trouve(s) :")
for _, it in ipairs(integrators) do
    print("  - " .. it.name)
end
print("")
print("Appuie sur chaque bouton un par un.")
print("Ctrl+T pour arreter.")
print("")

local function readAll()
    local state = {}
    for _, it in ipairs(integrators) do
        state[it.name] = {}
        for _, side in ipairs(SIDES) do
            state[it.name][side] = it.obj.getInput(side)
        end
    end
    return state
end

local prev = readAll()

while true do
    sleep(0.05)
    local current = readAll()
    for _, it in ipairs(integrators) do
        for _, side in ipairs(SIDES) do
            if current[it.name][side] and not prev[it.name][side] then
                print(it.name .. "  face: " .. side)
            end
        end
    end
    prev = current
end